package com.github.postsanf.yinian.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.ImageLocalSelectActivity;
import com.github.postsanf.yinian.adapter.LocalManageImageAdapter;


public class CameraAlbumFragment extends BaseFragment {
    private View view;
    private GridView mGirdViewbuttom;
    private LocalManageImageAdapter buttomAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerBroadcastReceiver();
    }

    // 广播接收
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "updatebuttomgridview":
                    buttomAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("updatebuttomgridview");
        getActivity().registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_cameraalbum, null);
        initView();
        return view;
    }

    private void initView() {
        mGirdViewbuttom = (GridView) view.findViewById(R.id.gv_image_buttom);
        buttomAdapter = new LocalManageImageAdapter(getActivity(), ImageLocalSelectActivity.mImgs, R.layout.yn_image_grid_item);
        mGirdViewbuttom.setAdapter(buttomAdapter);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mBroadcastReceiver);
    }
}
